package org.awa.demo.opencv.controller;

import lombok.extern.slf4j.Slf4j;
import org.awa.demo.opencv.bean.emqx.EmqxRequestForm;
import org.awa.demo.opencv.bean.request.LinkRequest;
import org.awa.demo.opencv.entity.GeoPolygonBuildEntity;
import org.awa.demo.opencv.entity.GeoPolygonRoadEntity;
import org.awa.demo.opencv.request.UploadLostRequest;
import org.awa.demo.opencv.service.IDemoService;
import org.awa.demo.opencv.task.kunming.GetKunMingPageRequest;
import org.awa.demo.opencv.task.kunming.GroupItemDto;
import org.awa.demo.opencv.task.kunming.GroupPointItemDto;
import org.awa.demo.opencv.util.R;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LinearRing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xxf
 */
@Slf4j
@RestController
@RequestMapping(value = "/demo")
public class DemoController {

    @Autowired
    private IDemoService demoService;

    @PostMapping(value = "/post")
    public R<String> testEmqxCallback(@RequestBody EmqxRequestForm form){
        log.info("testEmqxCallback > "+form);
        return R.ok();
    }

    /**
     * 导入geojson数据
     * @return
     */
    @PostMapping("/geojson")
    @ResponseBody
    public R<Object> send(@RequestParam("file") MultipartFile file) {
        log.info("进入[导入geojson]文件上传...");
        try {
            if (file.isEmpty()) {
                return R.fail("文件为空,请选择你的文件上传");
            }
            demoService.importGeoJson(file);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导入geojson data exception:"+e.toString(),e);
            return R.fail("fail:"+e.toString());
        }
    }

    @PostMapping("/link")
    public R<Object> getLink(@RequestBody LinkRequest request){
        GeometryFactory factory = JTSFactoryFinder.getGeometryFactory();
        Coordinate[] coordinates = new Coordinate[]{
                new Coordinate(request.getPointSrc().getLat(),request.getPointSrc().getLnt()),
                new Coordinate(request.getPointDes().getLat(),request.getPointDes().getLnt())
        };

        LinearRing linearRing = factory.createLinearRing(coordinates);
        return R.ok(linearRing.toText());
    }

    @GetMapping("/demo")
    public R<Object> getDemo(){
        demoService.calByNoise();
        return R.ok();
    }

    @GetMapping("/kunming")
    public R<Object> getKunMing() throws Exception{
        return R.ok(demoService.calByKunMing());
    }

    @GetMapping("/kunming/new")
    public R<Object> getKunMingNew() throws Exception{
        List<GroupItemDto> itemDtoList = demoService.calByKunMing();
        List<GroupPointItemDto> pointItemList = itemDtoList.stream().map(request -> {
            try{
                return demoService.calByKunMingNew(request);
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }).filter(item -> {
            return item != null;
        }).collect(Collectors.toList());
        return R.ok(pointItemList);
    }

    @GetMapping("/kunming/page")
    public R<Object> getKunMingPage(@Valid GetKunMingPageRequest request, BindingResult bindingResult) throws Exception{
        if(bindingResult.hasErrors()){
            return R.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        return R.ok(demoService.getKunMingPage(request.getTaskId(), request.getPNum()));
    }

}
